package com.nogroup.springbootproject.service.impl;

import com.nogroup.springbootproject.entity.Major;
import com.nogroup.springbootproject.dao.MajorDao;
import com.nogroup.springbootproject.service.MajorService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * (Major)表服务实现类
 *
 * @author makejava
 * @since 2024-04-07 18:45:33
 */
@Service("majorService")
@Transactional(rollbackFor = Exception.class)
public class MajorServiceImpl implements MajorService {
    @Resource
    private MajorDao majorDao;

    /**
     * 通过ID查询单条数据
     *
     * @param majorCode 主键
     * @return 实例对象
     */
    @Override
    public Major queryById(String majorCode) {
        return this.majorDao.queryById(majorCode);
    }

    /**
     * 分页查询
     *
     * @param major 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Major> queryByPage(Major major, PageRequest pageRequest) {
        long total = this.majorDao.count(major);
        return new PageImpl<>(this.majorDao.queryAllByLimit(major, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param major 实例对象
     * @return 实例对象
     */
    @Override
    public Major insert(Major major) {
        this.majorDao.insert(major);
        return major;
    }

    /**
     * 修改数据
     *
     * @param major 实例对象
     * @return 实例对象
     */
    @Override
    public Major update(Major major) {
        this.majorDao.update(major);
        return this.queryById(major.getMajorCode());
    }

    /**
     * 通过主键删除数据
     *
     * @param majorCode 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String majorCode) {
        return this.majorDao.deleteById(majorCode) > 0;
    }
}
